#ifndef WEB_INTERFACE_H
#define WEB_INTERFACE_H
#include <iostream>
#include "web_data.h"
#include <functional>

class WebInterface
{
public:
  WebInterface(WebBehaviorConfig *config = nullptr, int threadNum = 4);
  ~WebInterface();

  void start(int port);

  template <typename F, typename... Args>
  void registerHttp(HttpMethod method, const std::string path, F &&callback, Args &&...args)
  {
    auto boundArgs = std::make_tuple(args...);
    auto boundCallback = [callback, boundArgs](std::shared_ptr<HttpData> data)
    {
      std::apply([callback, data](Args... args)
                 { callback(data, args...); }, boundArgs);
    };

    initHttp(method, path, boundCallback);
  }

  template <typename F, typename... Args>
  void registerWS(WSMethod method, const std::string path, F &&callback, Args &&...args)
  {
    auto boundArgs = std::make_tuple(args...);
    auto boundCallback = [callback, boundArgs](std::shared_ptr<WsData> data)
    {
      std::apply([callback, data](Args... args)
                 { callback(data, args...); }, boundArgs);
    };
    initWs(method, path, boundCallback);
  }

private:
  void initHttp(HttpMethod method, const std::string path, std::function<void(std::shared_ptr<HttpData>)> func);
  void initWs(WSMethod method, const std::string path, std::function<void(std::shared_ptr<WsData>)> func);

private:
  void *m_handle;
  bool m_bSSL;
};

#endif